package com.sdx.common.utils;

import com.sdx.service.ConfigService;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName Utils
 * @Description: 常用工具
 * @author: tanbaocai
 * @date: 2018/7/30 14:27
 */
public class Utils {
	public static Date[] parseQueryTime(String queryStr) {
		Date[] result = null;
		if (!StringUtils.isEmpty(queryStr)) {
			String[] timePairs = queryStr.split(" ~ ");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String startStr = timePairs[0] + " 00:00:00";
		    result =new Date[timePairs.length];
			try {
				result[0] = df.parse(startStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (result.length == 2) {
				String endStr = timePairs[1] + " 23:59:59";
				try {
					result[1] = df.parse(endStr);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	/**
	 * 生成指定长度的随机数字，第一位不能为0
	 * @param length
	 * @return
	 */
	public static int getRandomIntByLength(int length) {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		int result = 0;
		for (int i = 0; i < length; i++) {
			if (i==0) {
				while(result==0) {
					result = random.nextInt(10);
				}
			} else {
				result = result*10+random.nextInt(10);
			}
		}
		return result;
	}
	/**
	 * 获取一定长度的随机字符串
	 * @param length 指定字符串长度
	 * @return 一定长度的字符串
	 */
	public static String getRandomStringByLength(int length) {
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	public static String imageUrl(String imagePath) {
		if (StringUtils.isEmpty(imagePath)) {
			return null;
		} else {
			if (imagePath.toLowerCase().startsWith("http")) {
				return imagePath;
			} else {
				return ConfigService.fileUrl + imagePath;
			}
		}
	}

	/**
	 * 字符串转list
	 * @param ids
	 * @param separator
	 * @return
	 */
	public static List<String> strToList(String ids,String separator) {
		if (ids.endsWith(separator)) {
			ids = StringUtils.substringBeforeLast(ids, separator);
		}
		List<String> idList = new ArrayList<String>();
		Collections.addAll(idList,ids.split(separator));
		return idList;
	}

	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		String randomUUIDString = uuid.toString().replaceAll("-","");
		return randomUUIDString;
	}

	public static byte[] input2byte(InputStream inStream)throws IOException {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[100];
		int rc = 0;
		while ((rc = inStream.read(buff, 0, 100)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		return in2b;
	}
}
